<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ include file="/include/taglibs.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<head>
    <title>功能管理</title>
    <link rel="stylesheet" href="${ctx}/assets/admin/LingUI/css/ui.jqgrid.css"/>
</head>

<div class="row">
    <div class="col-xs-12">
        <div id="search"></div>
        <%--查询条件更多 --%>
        <div class="b_grid_line  col-lg-12"></div>                        <!-- 分隔线 -->
        <table id="function_list_table" class="grid-table"></table>
        <%--jqgrid数据表格 --%>
        <div id="function_list_pager"></div>
        <%--jqgrid表格 页脚 --%>
    </div>
</div>
<script type="text/javascript">
    var scripts = [null,
        "${ctx}/assets/admin/LingUI/js/jqGrid/jquery.jqGrid.js",
        "${ctx}/assets/admin/LingUI/js/jqGrid/i18n/grid.locale-cn.js",
        "${ctx}/assets/admin/LingUI/js/control.js",
        "${ctx}/assets/admin/LingUI/js/jsutil/js/jquery.filterSearch.js",   //筛选搜索
        null];
    var _grid_id = "#function_list_table";	//表格 选择器
    var _pager_id = "#function_list_pager";	//表格页脚 选择器
    ace.load_ajax_scripts(scripts, function () {
        $("#search").strongFilterSearch({
            inputFiled: "FUNCTION_NAME",				//搜索字段，不填默认为searchValue
            inputPlaceholder: "请输入功能名称等关键字",		//占位符
            searchCallback: callback//,				//搜索按钮点击后回调
        });
        //搜索回调函数
        function callback(data) {
            jQuery("#function_list_table").jqGrid('setGridParam', {
                url: "${ctx}/${frameworkPath}/function/list?sort=FUNCTION_ORDER&&dir=asc",
                mtype: "POST",
                postData: data //传递的数据
            }).trigger("reloadGrid");
        }


        //调整以适应页面大小  其中 $(".page-content") 为jqgrid的父级容器
        $(window).off('resize.jqGrid').on('resize.jqGrid', function () {
            setTimeout(function () {
                $(_grid_id).jqGrid("setGridWidth", $(".page-content").width());
                $(".ui-jqgrid-bdiv").width($(".page-content").width() + 1);
            }, 50);
        });
        jQuery(_grid_id).jqGrid({
            mtype: "POST",
            url: "${ctx}/${frameworkPath}/function/list?sort=FUNCTION_ORDER&&dir=asc",
            colNames: ['编号', '功能名称', '所属模块', 'URL', '创建者', '创建时间', '排序字段', '操作'],
            colModel: [
                {name: 'id', index: 'id', width: 60, editable: true, hidden: false},
                {name: 'name', index: 'name', width: 60, editable: true},
                {name: 'moduleName', index: 'moduleName', width: 60, editable: true},
                {name: 'url', index: 'url', width: 80, editable: true},
                {name: 'creator', index: 'creator', width: 60, editable: true},
                {name: 'created', index: 'created', width: 60, editable: true, align: 'center'},
                {name: 'order', index: 'order', width: 30, editable: true, align: 'center'},
                {
                    name: 'action',
                    index: '',
                    width: 100,
                    fixed: false,
                    sortable: false,
                    align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        var s = '<div class=" action-buttons">'
                        s = s + '<a class="blue my-tooltip-link tooltip-primary" title="配置字段" style="cursor:pointer" ><i class="ace-icon fa fa-bars bigger-130 " onclick="editrow(\'' + rowObject.id + '\')"></i></a>';
                        s = s + '<a class="blue my-tooltip-link tooltip-primary" title="编辑" style="cursor:pointer" ><i class="ace-icon fa fa-pencil bigger-130 " onclick="editrow(\'' + rowObject.id + '\')"></i></a>';
                        s = s + '<a class="red my-tooltip-link tooltip-primary" title="删除" style="cursor:pointer"><i class="ace-icon fa fa-trash-o bigger-130"  onclick="deleterows(\'' + rowObject.id + '\')"></i></a>';
                        return s + '</div>';
                    }
                }
            ],
            pager: _pager_id, //分页信息 放置的位置
            page: lui.pageNum(_grid_id),
            sortname: 'moduleName',
            grouping: true,
            groupingView: {
                groupField: ['moduleName'],
                plusicon: 'fa fa-chevron-down bigger-110',
                minusicon: 'fa fa-chevron-up bigger-110'
            }
        });
        //自定义工具栏 =============BEGIN======================//
        jQuery(_grid_id)
                .jqGrid('navGrid', _pager_id)
                .navButtonAdd(_pager_id, {
                    title: "新增",//按钮悬浮提示
                    //iconTitle : "新增",//图标下面显示（不填的话默认为title里面的值）
                    buttonicon: "ace-icon fa fa-plus-circle purple",
                    onClickButton: function () {
                        lui.open({
                            title: '新增功能',
                            content: "${ctx}/${frameworkPath}/function/create",
                            area: ['700px', '400px'],
                            yes: function (index, layero) {
                                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                                var retObj = iframeWin.formValid();
                                if (retObj.text) {
                                    lui.info(retObj.text);
                                    if (retObj.state) {
                                        layer.close(index);
                                        jQuery(_grid_id).trigger("reloadGrid");
                                    }
                                }
                            }
                        });
                    }
                })
                .navButtonAdd(_pager_id, {
                    title: "编辑",
                    buttonicon: "ace-icon fa fa-pencil blue",
                    onClickButton: editrows
                })
                .navButtonAdd(_pager_id, {
                    title: "删除",
                    buttonicon: "ace-icon fa fa-trash-o red",
                    onClickButton: deleteRows
                })
                .navButtonAdd(_pager_id, {
                    title: "刷新",
                    buttonicon: "ace-icon fa fa-refresh orange",
                    onClickButton: function () {
                        //jQuery(_grid_id).trigger("reloadGrid");
                        callback();
                    }
                })
    });

    //编辑数据
    function editrow(id) {
        lui.open({
            title: '编辑功能',
            content: "${ctx}/${frameworkPath}/function/edit?id=" + id,
            area: ['700px', '400px'],
            yes: function (index, layero) {
                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                var retObj = iframeWin.formValid();
                if (retObj.text) {
                    lui.info(retObj.text);
                    if (retObj.state) {
                        layer.close(index);
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                }
            }
        });
    }

    function editrows() {
        var ids = $("#function_list_table").jqGrid('getGridParam', 'selarrrow');
        if (ids.length < 1) {
            lui.warn('至少选择一行数据');
            return;
        }
        else if (ids.length > 1) {
            lui.warn("最多选择一行数据");
            return;
        }
        lui.open({
            title: '编辑功能',
            content: "${ctx}/${frameworkPath}/function/edit?id=" + ids[0],
            area: ['600px', '300px'],
            yes: function (index, layero) {
                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                var retObj = iframeWin.formValid();
                if (retObj.text) {
                    lui.info(retObj.text);
                    if (retObj.state) {
                        layer.close(index);
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                }
            }
        });
    }

    //删除
    function deleterows(id) {
        lui.confirm('确定要删除吗？', function () {
            var idArray = new Array();
            idArray.push(id);
            $.ajax({
                type: 'POST',
                url: '${ctx}/${frameworkPath}/function/delete',
                traditional: true,
                dataType: 'json',
                data: {'ids': idArray},
                success: function (data) {
                    if (data.success) {
                        lui.success('删除成功');
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                    else {
                        lui.fail('删除失败');
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    lui.fail('删除失败');
                }
            });
        }, function () {

        });
    }

    function deleteRows() {
        var ids = $("#function_list_table").jqGrid('getGridParam', 'selarrrow');
        if (ids.length < 1) {
            lui.info('至少选择一行数据');
            return;
        }
        lui.confirm('确定要删除吗？', function () {
            var idArray = new Array();
            for (var i = 0; i < ids.length; i++) {
                idArray.push(ids[i]);
            }
            $.ajax({
                type: 'POST',
                url: '${ctx}/${frameworkPath}/function/delete',
                traditional: true,
                dataType: 'json',
                data: {'ids': idArray},
                success: function (data) {
                    if (data.success) {
                        lui.success('删除成功');
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                    else {
                        lui.success('删除失败');
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    lui.success('删除失败');
                }
            });
        }, function () {
        });
    }
</script>